package model.data.tableModel.inventory;

import java.io.File;

import model.data.interfaces.IWriteableToDB;
import model.data.tableModel.TableControl;
import model.handler.databaseHandling.QueryFactory;
import enums.Status;

/**
 * Representing an inventory at a given date.
 */
public final class Inventory implements IWriteableToDB{

	private final int inventoryID;
	private final String date;
	private final String comment;
	private Status status;
	private File file;
	
	/**
	 * Private constructor because it should be instantiated with the given factory methods.
	 * @param inventoryID ID of the inventory
	 * @param date Date of the inventory
	 * @param comment 
	 * @param status
	 * @param file
	 */
	private Inventory(int inventoryID, String date, String comment, Status status, File file) {
		assert inventoryID >= 0;
		
		this.inventoryID = inventoryID;
		this.date = date;
		this.comment = comment;
		this.status = status;
	}
	
	/**
	 * Create a new instance of Inventory.
	 * @param date Date of the inventory
	 * @param comment Comment that was entered
	 * @param status Status of the inventory
	 * @param pdf File that was created
	 * @return Inventory instance
	 */
	public static Inventory createNewInventory(String date, String comment, Status status, File pdf){
		return new Inventory(TableControl.getCentralCounter(), date, comment, status, pdf);
	}
	
	/**
	 * Create an already existing instance of Inventory.
	 * @param inventoryID ID of the inventory
	 * @param date Date of the inventory
	 * @param comment Comment that was entered
	 * @param status Status of the inventory
	 * @param pdf File that was created
	 * @return Inventory instance
	 */
	public static Inventory createExistingInventory(int inventoryID, String date, String comment, Status status, File file){
		return new Inventory(inventoryID, date, comment, status, file);
	}	

	@Override
	public boolean writeToDB() {
		return QueryFactory.writeInventoryToDB(false, inventoryID, date, comment, status, file);
	}

	@Override
	public boolean writeUpdateToDB() {
		return QueryFactory.writeInventoryToDB(true, inventoryID, date, comment, status, file);
	}

	public String getDate() {
		return date;
	}

	public void setFile(File pdf) {
		this.file = pdf;
	}

	public String getComment() {
		return comment;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + inventoryID;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Inventory other = (Inventory) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (inventoryID != other.inventoryID)
			return false;
		return true;
	}
}
